package com.mt.roll.process.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.mt.roll.core.api.Assert;
import com.mt.roll.core.base.BaseService;
import com.mt.roll.core.dal.entity.*;
import com.mt.roll.core.dal.service.*;
import com.mt.roll.core.errorcode.ApiErrorCode;
import com.mt.roll.core.params.IdParam;
import com.mt.roll.core.utils.CodeGeneratorHelper;
import com.mt.roll.core.vo.IdListVo;
import com.mt.roll.core.vo.IdVo;
import com.mt.roll.core.vo.R;
import com.mt.roll.process.mapper.ProcessFlowEquipmentVoMapper;
import com.mt.roll.process.params.ProcessFlowEquipmentListParam;
import com.mt.roll.process.params.ProcessFlowEquipmentParam;
import com.mt.roll.process.params.ProcessFlowEquipmentQueryParam;
import com.mt.roll.process.params.ProcessFlowSequenceEquipmentParam;
import com.mt.roll.process.service.ProcessFlowEquipmentService;
import com.mt.roll.process.vo.*;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author: liguanghao
 * @Date: 2020/12/31 15:37
 * @Version 1.0
 */
@Service
@Transactional
public class ProcessFlowEquipmentServiceImpl extends BaseService implements ProcessFlowEquipmentService {

    @Resource
    private ProcessFlowEquipmentServiceBiz processFlowEquipmentServiceBiz;
    @Resource
    private EquipmentRecipeServiceBiz equipmentRecipeServiceBiz;
    @Resource
    private EquipmentBomServiceBiz equipmentBomServiceBiz;
    @Resource
    private ProcessFlowEquipmentVoMapper processFlowEquipmentVoMapper;
    @Resource
    private ProcessFlowSequenceServiceBiz processFlowSequenceServiceBiz;
    @Resource
    private EquipmentServiceBiz equipmentServiceBiz;
    @Override
    public R<ProcessFlowEquipmentVo> get(IdParam idParam) {
        Assert.notNull(ApiErrorCode.INVALID_PARAMETER,idParam.getId());
        ProcessFlowEquipment processFlowEquipment = processFlowEquipmentServiceBiz.getById(idParam.getId());
        ProcessFlowEquipmentVo processFlowEquipmentVo=ProcessFlowEquipmentVo.builder().build();
        BeanUtils.copyProperties(processFlowEquipmentVo,processFlowEquipment);
        //@TODO 配方详情、bom详情，完成设备配方管理后再加上
        return successful(processFlowEquipmentVo);
    }

    @Override
    public R<List<WorkSequenceEquipmentVo>> list(ProcessFlowEquipmentQueryParam processFlowEquipmentQueryParam) {
        Assert.notNull(ApiErrorCode.INVALID_PARAMETER,processFlowEquipmentQueryParam.getProcessFlowId());
        //List<ProcessFlowEquipmentVo> processFlowEquipmentVos = (List<ProcessFlowEquipmentVo>) processFlowEquipmentVoMapper.getProcessFlowEquipmentVoList(processFlowEquipmentQueryParam.getProcessFlowId());
        List<WorkSequenceEquipmentVo> workSequenceVoList = processFlowEquipmentVoMapper.getWorkSequenceVo(processFlowEquipmentQueryParam.getProcessFlowId());
        if (workSequenceVoList!=null){
            for (WorkSequenceEquipmentVo workSequenceEquipmentVo:workSequenceVoList
                 ) {
                List<ProcessFlowEquipmentVo> processFlowEquipmentParams= processFlowEquipmentVoMapper.getProcessFlowEquipmentVoList(workSequenceEquipmentVo.getWorkSequenceId(),processFlowEquipmentQueryParam.getProcessFlowId());
                workSequenceEquipmentVo.setProcessFlowEquipmentParams(processFlowEquipmentParams);
                workSequenceEquipmentVo.setProcessFlowId(processFlowEquipmentQueryParam.getProcessFlowId());
            }
        }
        return successful(workSequenceVoList);
    }

    @Override
    public R<IdListVo> add(ProcessFlowEquipmentListParam processFlowEquipmentListParam) {
        List<ProcessFlowSequenceEquipmentParam> processFlowSequenceEquipmentParams = processFlowEquipmentListParam.getProcessFlowSequenceEquipmentParams();
        Assert.notNull("设备点数据不能为空",processFlowSequenceEquipmentParams);
        Long processFlowId = processFlowSequenceEquipmentParams.get(0).getProcessFlowId();
        //删除原有的设备点列表
        QueryWrapper<ProcessFlowEquipment> wrapper=new QueryWrapper<>();
        wrapper.eq(ProcessFlowEquipment.PROCESS_FLOW_ID,processFlowId);
        processFlowEquipmentServiceBiz.remove(wrapper);//删除原有设备点信息
        QueryWrapper<ProcessFlowSequence> sequenceQueryWrapper=new QueryWrapper<>();
        sequenceQueryWrapper.eq(ProcessFlowSequence.PROCESS_FLOW_ID,processFlowId);
        processFlowSequenceServiceBiz.remove(sequenceQueryWrapper);//删除原有工艺工序关联信息
        List<Long> idList=new ArrayList<>();
        if (processFlowEquipmentListParam.getProcessFlowSequenceEquipmentParams()!=null){
            for (ProcessFlowSequenceEquipmentParam processFlowSequenceEquipmentParam:processFlowEquipmentListParam.getProcessFlowSequenceEquipmentParams()
                 ) {
                Long workSequenceId = processFlowSequenceEquipmentParam.getWorkSequenceId();
                //添加新的工艺工序关联关系
                ProcessFlowSequence processFlowSequence=new ProcessFlowSequence();
                processFlowSequence.setProcessFlowId(processFlowId);
                processFlowSequence.setWorkSequenceId(workSequenceId);
                processFlowSequenceServiceBiz.save(processFlowSequence);
                //添加设备点
                if (processFlowSequenceEquipmentParam.getProcessFlowEquipmentParams().size()!=0){
                    List<ProcessFlowEquipment> processFlowEquipments = com.mt.roll.core.utils.BeanUtils.copyList(processFlowSequenceEquipmentParam.getProcessFlowEquipmentParams(), ProcessFlowEquipment.class);

                    for (ProcessFlowEquipment processFlowEquipment:processFlowEquipments
                    ) {
                        processFlowEquipment.setSequenceId(workSequenceId);
                        processFlowEquipment.setCode(CodeGeneratorHelper.getProcessFlowEquipmentGroupCode());
                        setCommonField(processFlowEquipment);
                        idList.add(processFlowEquipment.getId());
                    }
                    processFlowEquipmentServiceBiz.saveBatch(processFlowEquipments);
                }
            }

        }
        return successful(IdListVo.builder().ids(idList).build());
    }

    @Override
    public R<IdVo> update(ProcessFlowEquipmentParam processFlowEquipmentParam) {
        return null;
    }

    @Override
    public R<IdVo> delete(IdParam idParam) {
        return null;
    }

    @Override
    public R<List<EquipmentRecipeVo>> equipmentRecipeList(IdParam idParam) {
        Assert.notNull(ApiErrorCode.INVALID_PARAMETER,idParam.getId());
        QueryWrapper<EquipmentRecipe> wrapper=new QueryWrapper<>();
        wrapper.eq(EquipmentRecipe.EQUIPMENT_ID,idParam.getId());
        List<EquipmentRecipe> equipmentRecipeList = equipmentRecipeServiceBiz.list(wrapper);
        List<EquipmentRecipeVo> equipmentRecipeVos = com.mt.roll.core.utils.BeanUtils.copyList(equipmentRecipeList, EquipmentRecipeVo.class);
        return successful(equipmentRecipeVos);
    }

    @Override
    public R<List<EquipmentBomVo>> equipmentBomList(IdParam idParam) {
        Assert.notNull(ApiErrorCode.INVALID_PARAMETER,idParam.getId());
        QueryWrapper<EquipmentBom> wrapper=new QueryWrapper<>();
        wrapper.eq(EquipmentBom.EQUIPMENT_ID,idParam.getId());
        List<EquipmentBom> equipmentBomList = equipmentBomServiceBiz.list(wrapper);
        List<EquipmentBomVo> equipmentBomVos = com.mt.roll.core.utils.BeanUtils.copyList(equipmentBomList, EquipmentBomVo.class);
        return successful(equipmentBomVos);
    }

    @Override
    public R<List<EquipmentRecipeVo>> allEquipmentRecipeList() {
        List<EquipmentRecipe> equipmentRecipeList = equipmentRecipeServiceBiz.list();
        List<EquipmentRecipeVo> equipmentRecipeVos = com.mt.roll.core.utils.BeanUtils.copyList(equipmentRecipeList, EquipmentRecipeVo.class);
        return successful(equipmentRecipeVos);
    }

    @Override
    public R<List<EquipmentBomVo>> allEquipmentBomList() {
        List<EquipmentBom> equipmentBomList = equipmentBomServiceBiz.list();
        List<EquipmentBomVo> equipmentBomVos = com.mt.roll.core.utils.BeanUtils.copyList(equipmentBomList, EquipmentBomVo.class);
        return successful(equipmentBomVos);
    }

    @Override
    public R<List<EquipmentVo>> equipmentList() {
        List<Equipment> equipmentList = equipmentServiceBiz.list();
        List<EquipmentVo> equipmentVos = com.mt.roll.core.utils.BeanUtils.copyList(equipmentList, EquipmentVo.class);
        return successful(equipmentVos);
    }
}
